	package com.erpanderp.wam.ac.model.service.reimbursement;
	
	import java.util.ArrayList;
import java.util.List;
	
import org.hibernate.FlushMode;
	import org.hibernate.HibernateException;
	import org.hibernate.Query;
	import org.hibernate.Session;
	
import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpenseDetails;
import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;
	
	public class MiscReimbursementDAO {
	
	     
	     public String getLastGeneratedMiscExpenseFormCode(String employeeCode){	    	
	   	  Long count = null;	 		
	   	 	try{
	   	 			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	   	 			session.getTransaction().begin();
	   	 			String queryString = "select count(*) from MiscExpense me where me.employee.employeeCode=:employeeCode";
	   	
	   	 			Query query = session.createQuery(queryString);
	   	 			query.setCacheable(false).setString("employeeCode",employeeCode);;
	   	 			count = (Long) query.uniqueResult();
	   	 			session.getTransaction().commit();
	   	 		} catch (HibernateException e) {
	   	 			e.printStackTrace();
	   	 		}
	   	 		
	   	 		count = count+1;
	   	 		return count.toString();	 		
	   	     }
	   
	     public MiscExpense createMiscExpenseForm(MiscExpense miscExpense){
	    	 try{
	    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	    		 session.setFlushMode(FlushMode.COMMIT);
	    		 session.beginTransaction();
	    		 session.saveOrUpdate(miscExpense);
	    		 session.getTransaction().commit();
	    	 }catch(Exception e){
	    		 e.printStackTrace();
	    		 return null;
	    	 }
	    	 return miscExpense;
	    	 
	     }
	     public MiscExpense getMiscExpense(String miscExpenseCode , Long employeeId) {
	    	 System.out.println("IN GET MISC EXPENSE");
	    	 MiscExpense miscExpense = null;
	 		try {
	 			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	 			session.beginTransaction();
	 			session.setFlushMode(FlushMode.COMMIT);
	 			Query query = session.createQuery("from MiscExpense me where me.miscExpenseCode = :miscExpenseCode and me.employee.employeeId =:employeeId");
	 			System.out.println(query == null);
	 			query.setCacheable(false).setString("miscExpenseCode",	miscExpenseCode).setLong("employeeId", employeeId);
	 			miscExpense = (MiscExpense) query.uniqueResult();
	 			session.getTransaction().commit();
	 			
	 			return miscExpense;
	 		} catch (Exception e) {
	 			e.printStackTrace();
	 			return null;
	 		}
	 	}
	     public MiscExpense getMiscExpense(String miscExpenseCode) {
	    	 System.out.println("IN GET MISC EXPENSE");
	    	 MiscExpense miscExpense = null;
	 		try {
	 			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	 			session.beginTransaction();
	 			session.setFlushMode(FlushMode.COMMIT);
	 			Query query = session.createQuery("from MiscExpense me where me.miscExpenseCode = :miscExpenseCode");
	 			System.out.println(query == null);
	 			query.setCacheable(false).setString("miscExpenseCode",	miscExpenseCode);
	 			List temp = (List<MiscExpense>) query.list();
	 			session.getTransaction().commit();
	 			if(temp != null && temp.size() > 0){
	 				return (MiscExpense) temp.get(0);
	 			}
	 			return null; 			
	 			
	 		} catch (Exception e) {
	 			e.printStackTrace();
	 			return null;
	 		}
	 	}
	     
	    public List<MiscExpenseDetails> getMiscExpensesListByMisceExpenseId(Long miscExpenseId){
	    	List<MiscExpenseDetails> miscExpenseDetails = null;
	    	try{
	    		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("from MiscExpense me where me.miscExpenseId = :miscExpenseId");
				System.out.println(query == null);
				query.setCacheable(false).setLong("miscExpenseId",	miscExpenseId);
				miscExpenseDetails = (List<MiscExpenseDetails>) query.list();
				session.getTransaction().commit();
				
				return miscExpenseDetails;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	    	 
	     }
	    public List<MiscExpense> getMiscExpensesListByWAMUser(Long employeeId){
	    	System.out.println("IN GET MIS EXPENSE LIST BY WAM USER");
	    	List<MiscExpense> miscExpenseList = null;
	    	try{
	    		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("from MiscExpense me where me.employee.employeeId = :employeeId");
				System.out.println(query == null);
				query.setCacheable(false).setLong("employeeId",	employeeId);
				miscExpenseList = (List<MiscExpense>) query.list();
				session.getTransaction().commit();
				
				return miscExpenseList;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
	    	 
	     }
	    public MiscExpense updateMiscExpenseForm(MiscExpense miscExpense) {
	    	 try{
	    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	    		 session.setFlushMode(FlushMode.AUTO);
	    		 session.beginTransaction();
	    		 session.saveOrUpdate(miscExpense);
	    		 session.getTransaction().commit();
	    	 }catch(Exception e){
	    		 e.printStackTrace();
	    		 return null;
	    	 }
	    	 return miscExpense;
	    
	 }
	    
	    //DELETE METHOD
		 public MiscExpenseDetails deleteMiscExpensDetailseById(MiscExpenseDetails miscExpenseDetails)
			{
				try{
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					session.beginTransaction();
					session.setFlushMode(FlushMode.COMMIT);
					session.delete(miscExpenseDetails);
					session.getTransaction().commit();
					return miscExpenseDetails;
					
				}catch(Exception e){
					e.printStackTrace();
					return null;
				}		
			}
		 
		 //For getting inprocess/approved/rejected records for approver
		 public List<MiscExpense> getMiscExpenseRequestsByApproverId(Long employeeId, String status) {
				List<MiscExpense> miscExpenses = null;
				try {
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					session.beginTransaction();
					session.setFlushMode(FlushMode.COMMIT);
					
					String queryString = "from MiscExpense me where me.approvedBy.employeeId = :employeeId and me.employee.status=:employeeStatus";
					if(status != null && !status.equals(""))
						queryString += " and me.expenseStatus = :status ";
					
					queryString += " order by me.requestDate desc";
					Query query = session.createQuery(queryString);
					query.setCacheable(false).setLong("employeeId",	employeeId).setString("employeeStatus", CommonLookup.STATUS_ACTIVE);
					if(status != null && !status.equals(""))
						query.setString("status", status);
					
					miscExpenses = (List<MiscExpense>) query.list();
					session.getTransaction().commit();
					return miscExpenses;
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}
		 
		 
		 public List<MiscExpense> getAllMiscExpenses(Integer year,Integer month,String expenseStatus){
			 System.out.println("===month====DAO=="+month);
			 System.out.println("==year=====DAO=="+year);
			 System.out.println("=expenseStatus======DAO=="+expenseStatus);
		 List<MiscExpense> miscExpenses = new ArrayList<MiscExpense>();
					try{
						Session session = HibernateUtil.getSessionFactory().getCurrentSession();
						session.beginTransaction();
						session.setFlushMode(FlushMode.COMMIT);
						String queryString = "from MiscExpense me where me.status =:status and MONTH(me.requestDate)=:month  and YEAR(me.requestDate)=:year ";
																	
						if(expenseStatus != null && !expenseStatus.equals("")){
							queryString += "and me.expenseStatus=:expenseStatus ";
						}
						queryString += "order by me.requestDate desc";
						Query query = session.createQuery(queryString);
																		
						if(expenseStatus != null && !expenseStatus.equals("")){
							query.setString("expenseStatus", expenseStatus);
						}	
						
						query.setCacheable(false).setString("status", CommonLookup.STATUS_ACTIVE).setInteger("year", year).setInteger("month", month);
						
						miscExpenses =(List<MiscExpense>) query.list();
						System.out.println("====miscExpenses SIZE============DAO======"+miscExpenses.size());
						session.getTransaction().commit();
						return miscExpenses;	
					}catch(Exception e){
						e.printStackTrace();
						return null;
					}		
				}
		
}
